<?php
/*
	+-------------------------------------
	+	基础类，Mysql数据库通用开发组件
	+-------------------------------------
	+	版权所有
	+-------------------------------------
*/
class Mysql{
	
	public $dbhost;
	public $dbuser;
	public $dbpassword;
	public $dbname;
	
	function __construct($dbhost,$dbuser,$dbpassword,$dbname){
		$this->dbhost = $dbhost;
		$this->dbuser = $dbuser;
		$this->dbpassword = $dbpassword;
		$this->dbname = $dbname;
		
		$this->connect();
		$this->select($this->dbname);
	}
	
	public function connect(){
		return mysql_connect($this->dbhost,$this->dbuser,$this->dbpassword);		
	}
	
	public function select(){
		return mysql_select_db($this->dbname);
	}


	public function query($sql){
		return mysql_query($sql);	
	}
	
	public function fetch($query){
		return mysql_fetch_array($query);
	}
	
	public function fetchAllQuery($sql){
		$data = $this->query($sql);
		while($result = mysql_fetch_array($data)){
			$res[] = $result;	
		}	
		return $res;
	}
	
	public function rows($query){
		return mysql_num_rows($query);	
	}
		
	public function fetchAll($tablename){
		$data = $this->query("SELECT * FROM `".$tablename."`");
		while($result = mysql_fetch_array($data)){
			$res[] = $result;	
		}	
		return $res;
	}
	
	public function rowsAll($tablename){
		return mysql_num_rows($this->query("SELECT * FROM `".$tablename."`"));
	}
	
	public function get_where($tablename,$where_array){
		list($key,$value) = each($where_array);
		$query = $this->query("SELECT * FROM `".$tablename."` WHERE `".$key."` = ".$value."");	
		while($result = mysql_fetch_array($query,MYSQL_ASSOC)){
			$res[] = $result;	
		}	
		return $res;
	}
	
	public function check_fields($tablename,$array){
		$fields = $this->get_fields($tablename);
		foreach($array as $k => $v){
			if(!in_array($k,$fields)){
				return false;
			}	
		}
	}
	
	public function get_fields($table)
	{
		$fields = array();
		$result = $this->query("SHOW COLUMNS FROM $table");
		while($r = mysql_fetch_array($result))
		{
			$fields[] = $r['Field'];
		}
		$this->free_result($result);
		return $fields;
	}

	
	public function insert($tablename,$data_array){
		$this->check_fields($tablename,$data_array);
		return $this->query("INSERT INTO `".$tablename."`(`".implode('`,`', array_keys($data_array))."`) VALUES('".implode("','", $data_array)."')");
	}
	
	
	public function update($tablename,$data_array,$where_array){
		$this->check_fields($tablename,$data_array);
		if($where_array){
			$sql = "";
			foreach($data_array as $k => $v){
				$sql .= ", `$k`='$v'";	
			}
			$sql = substr($sql,1);
			list($key,$value) = each($where_array);
			$sql = "UPDATE `".$tablename."` SET ".$sql." WHERE `".$key."` = ".$value."";
		}else{
			$sql = "REPLACE INTO `".$tablename."`(`".implode('`,`', array_keys($data_array))."`) VALUES('".implode("','", $data_array)."')";
		}
		return $this->query($sql);
	}
	
	public function delete($tablename,$where_array){
		list($key,$value) = each($where_array);
		return $this->query("DELETE FROM `".$tablename."` WHERE `".$key."` = ".$value."");	
	}
	
	public function free_result(&$query)
	{
		return mysql_free_result($query);
	}

}
?>